<?php
class Category extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		
	}
	public function getAll($intAdType, $intType, $intStatus, $strStart, $strEnd, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call categories_getall_a(?,?,?,?,?,
													 ?,?)');					
			$this->bindParam(1, $intAdType, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intType, PDO::PARAM_INT, 4);			
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(5, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(6, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function get($intCategoryId) {
		try {	
			$this->prepare('call categories_get(?)');		
			$this->bindParam(1, $intCategoryId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::get - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getCategory($intType, $intStore=1) {
		try {	
			$this->prepare('call categories_getall_type(?,?)');		
			$this->bindParam(1, $intType, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intStore, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::getCategory - ' .$e->getMessage());			
			return null;
		}
	}
	public function getParent($intType) {
		try {	
			$this->prepare('call categories_getall_parent(?)');		
			$this->bindParam(1, $intType, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::getParent - ' .$e->getMessage());			
			return null;
		}
	}
	public function getChild($intParentId) {
		try {	
			$this->prepare('call categories_getall_child(?)');		
			$this->bindParam(1, $intParentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::getChild - ' .$e->getMessage());			
			return null;
		}
	}
	/**/
	public function add($intLoginId, $intStoreId, $strName, $strSlug, $strShort,
						$strIcon, $strIconAct, $intParentId, $intIseq, $intType=0,						
						$strTag='', $intAdv=0) {
		try {
			$this->prepare('call categories_add(?,?,?,?,?,
												?,?,?,?,?,
												?,?)');
			$this->bindParam(1, $intLoginId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intStoreId		, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName		, PDO::PARAM_STR, 150);			
			$this->bindParam(4, $strSlug		, PDO::PARAM_STR, 255);			
			$this->bindParam(5, $strShort		, PDO::PARAM_STR, 255);			
			$this->bindParam(6, $strIcon		, PDO::PARAM_STR, 150);
			$this->bindParam(7, $strIconAct		, PDO::PARAM_STR, 150);			
			$this->bindParam(8, $intParentId	, PDO::PARAM_INT, 11);
			$this->bindParam(9,	$intIseq		, PDO::PARAM_INT, 11);
			$this->bindParam(10, $intType		, PDO::PARAM_INT, 4);
			$this->bindParam(11, $strTag		, PDO::PARAM_STR, 100);
			$this->bindParam(12, $intAdv		, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {	
				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::add - ' . $e->getMessage());
			return null;
		}
	}		
	public function update($intLoginId, $intCategoryId, $intStoreId, $strName, $strSlug,
						   $strShort, $strIcon, $strIconAct, $intIseq, $intType=0,
						   $strTag='', $intAdv=0) {
		try {
			$this->prepare('call categories_update(?,?,?,?,?,
												   ?,?,?,?,?,
												   ?,?)');
			$this->bindParam(1, $intLoginId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStoreId		, PDO::PARAM_INT, 11);
			$this->bindParam(4, $strName		, PDO::PARAM_STR, 150);						
			$this->bindParam(5, $strSlug		, PDO::PARAM_STR, 255);			
			$this->bindParam(6, $strShort		, PDO::PARAM_STR, 255);			
			$this->bindParam(7, $strIcon		, PDO::PARAM_STR, 150);
			$this->bindParam(8, $strIconAct		, PDO::PARAM_STR, 150);			
			$this->bindParam(9,	$intIseq		, PDO::PARAM_INT, 11);
			$this->bindParam(10, $intType		, PDO::PARAM_INT, 4);
			$this->bindParam(11, $strTag		, PDO::PARAM_STR, 100);			
			$this->bindParam(12, $intAdv		, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				
				$strOldIcon 	= isset($arrResult[0]['icon']) ? $arrResult[0]['icon'] : '';
				$strOldIconAct 	= isset($arrResult[0]['icon_act']) ? $arrResult[0]['icon_act'] : '';
				if($strIcon != '') {
					$this->deleteImg($strOldIcon);
				}
				if($strIconAct != '') {
					$this->deleteImg($strOldIconAct);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::update - ' . $e->getMessage());
			return null;
		}
	}
	public function updateStatus($intLoginId, $intCategoryId, $intStatus, $intPerm) {
		try {				
			$this->prepare('call categories_update_status(?,?,?,?)');
			$this->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intStoreId = isset($arrResult[0]['storeid']) ? $arrResult[0]['storeid'] : 0;
				$intType = isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;
				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}
	public function delete($intLoginId, $intCategoryId) {
		try {			
			$this->prepare('call categories_delete(?,?)');
			$this->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strIcon 	= isset($arrResult[0]['icon']) ? $arrResult[0]['icon'] : '';
				$strIconAct = isset($arrResult[0]['icon_act']) ? $arrResult[0]['icon_act'] : '';
				$intStoreId = isset($arrResult[0]['storeid']) ? $arrResult[0]['storeid'] : 0;
				$intType 	= isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;
				
				if($strIcon != '') {
					$this->deleteImg($strIcon);
				}
				if($strIconAct != '') {
					$this->deleteImg($strIconAct);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Category::delete - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteImg($strIcon) {	
		if(file_exists(DIR_CATEGORY . $strIcon))
			@unlink(DIR_CATEGORY . $strIcon);
	}	
}
